package com.testmanager.testmananger.shuiyou.common.util;


import com.alibaba.fastjson.JSONObject;
import com.testmanager.testmananger.shuiyou.common.Constant.RedisFileName;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Configuration
@Slf4j
public class JUDANRedisFileManager {

    @Autowired
    OtherRedis otherRedis;

//    @Resource(name="redisOther")
//    RedisTemplate<String,Object> redisTemplate;


    public JSONObject getRedisFIlesJSONObject(){
        log.info("初始化bean:juduanFilesJSONObject");
        JSONObject json = new JSONObject();
        log.info("base:{}",otherRedis.getRedisTemplate().opsForValue().get(RedisFileName.BASE_BASE_DATA_NAME).toString());
        log.info("cwbb:{}",otherRedis.getRedisTemplate().opsForValue().get(RedisFileName.BASE_CWBB_DATA_NAME).toString());

        json.put(RedisFileName.BASE,strToJsonOfJufan(otherRedis.getRedisTemplate().opsForValue().get(RedisFileName.BASE_BASE_DATA_NAME).toString()));

        json.put(RedisFileName.CWBB,strToJsonOfJufan(otherRedis.getRedisTemplate().opsForValue().get(RedisFileName.BASE_CWBB_DATA_NAME).toString()));

        return json;
    }







   // @Bean("juduanFilesString")
    public Map<String,String> getRedisFiles(){
        log.info("初始化bean:juduanFilesString");

        Map<String,String> map = new HashMap();
        map.put(RedisFileName.BASE,otherRedis.getRedisTemplate().opsForValue().get(RedisFileName.BASE_BASE_DATA_NAME).toString());
        map.put(RedisFileName.CWBB,otherRedis.getRedisTemplate().opsForValue().get(RedisFileName.BASE_CWBB_DATA_NAME).toString());
        return map;
    }

    public  JSONObject strToJsonOfJufan(String content){
        JSONObject contentObject = JSONObject.parseObject(content);
        String tempstr = "";
        JSONObject tempJson = null;
        JSONObject contentdataType = new JSONObject();
        for (String key : contentObject.keySet()){
            tempJson = JSONObject.parseObject(contentObject.getString(key));
            contentdataType.put(key,tempJson.getString("dataType"));
            tempstr = tempJson.getString("data");


            contentObject.put(key, isJsonValid(tempstr)?JSONObject.parseObject(tempstr):tempstr);
        }
        contentObject.put("dataType",contentdataType);

        return contentObject;
    }

    private  boolean isJsonValid(String s){
        try {
            JSONObject.parseObject(s);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    public static String jsonToStrOfJufan(JSONObject json){
        JSONObject newjson = new JSONObject();
        // System.out.println(json.keySet().size());
        for (String key : json.keySet()){
            if ("dataType".equals(key)) continue;
            JSONObject data = new JSONObject();
            data.put("data",json.getString(key));
            data.put("dataType",json.getJSONObject("dataType").getString(key));
            newjson.put(key,data.toJSONString());
        }
        return newjson.toJSONString();
    }



}
